﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.Http;
using System.Globalization;
using System.Threading;

namespace JYH.Web.MES
{
    public class Global : HttpApplication
    {
        void Application_Start(object sender, EventArgs e)
        {

           
            // 在应用程序启动时运行的代码
            GlobalConfiguration.Configure(WebApiConfig.Register);
        }
        protected void Application_BeginRequest()
        {
            var cookie = Request.Cookies["userLang"];
            if (cookie != null)
            {
                var culture = new CultureInfo(cookie.Value);
                Thread.CurrentThread.CurrentUICulture = culture;
                Thread.CurrentThread.CurrentCulture = culture;
            }
            else
            {
                var userLang = Request.Cookies["userLang"]?.Value;

                if (string.IsNullOrEmpty(userLang))
                {
                    // 2. 自动检测浏览器语言
                    var browserLanguages = Request.UserLanguages;
                    if (browserLanguages != null && browserLanguages.Length > 0)
                    {
                        // 获取浏览器首选语言（如 "zh-CN", "en-US"）
                        userLang = browserLanguages[0].ToString();

                        // 3. 匹配支持的语言

                    }
                    else
                    {
                        userLang = "zh-CN";       // 无语言首选项时默认英文
                    }
                }


                Thread.CurrentThread.CurrentUICulture = new CultureInfo(userLang);
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(userLang);
                // 5. 可选：将语言设置存入Cookie（持久化）
                Response.Cookies.Add(new HttpCookie("userLang", userLang) { Expires = DateTime.Now.AddMonths(1) });
            }
        }
    }
}